package servlet;

import entity.Monster2;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * jsp + servlet + jstl + el完成查询-显示案例
 */
public class QueryServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("QueryServlet被调用....");

        //1.准备数据
        Monster2 monster = new Monster2(100, "小妖怪", "巡山", 345.7);
        Monster2 monster2 = new Monster2(200, "大妖怪", "做饭", 1345.7);
        Monster2 monster3 = new Monster2(300, "老妖怪", "打扫卫生", 11345.7);
        ArrayList<Monster2> list = new ArrayList<>();
        list.add(monster);
        list.add(monster2);
        list.add(monster3);

        //2.存入 req 域对象
        req.setAttribute("monsters", list);

        //3.请求转发到显示页面
        req.getRequestDispatcher("/exercise/show.jsp").forward(req, resp);

    }
}
